package com.henry.chatkit.glide

import android.graphics.Bitmap
import android.widget.ImageView
import androidx.annotation.DrawableRes
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.henry.chatkit.R
import com.henry.chatkit.utils.appContext
import jp.wasabeef.glide.transformations.RoundedCornersTransformation


object GlideEngine {

    fun loadBitmap(imageUrl: Any, targetImageSize: Int): Bitmap {
        return Glide.with(appContext()).asBitmap()
                .load(imageUrl)
                .apply(RequestOptions().error(R.drawable.default_head))
                .submit(targetImageSize, targetImageSize)
                .get()
    }

    fun loadImage(imageUrl: Any?, imageView: ImageView) {
        Glide.with(imageView).load(imageUrl).into(imageView)
    }

    fun loadRoundImage(imageUrl: Any?, imageView: ImageView, radius: Int, @DrawableRes holder: Int? = null) {
        if (imageUrl == null && holder != null) {
            imageView.setImageResource(holder)
        } else if (holder == null) {
            Glide.with(imageView).load(imageUrl).apply(RequestOptions.bitmapTransform(RoundedCornersTransformation(radius, 0))).into(imageView)
        } else {
            Glide.with(imageView).load(imageUrl).apply(
                    RequestOptions().transform(RoundedCornersTransformation(radius, 0))
                            .placeholder(holder))
                    .into(imageView)
        }
    }

    @JvmStatic
    fun loadImage(imageUrl: String, imageView: ImageView) {
        Glide.with(imageView).load(imageUrl).into(imageView)
    }

}